package com.wwdx.service.impl;

import com.google.common.collect.Lists;
import com.wwdx.BaseCommons;
import com.wwdx.model.WwdxBookModel;
import com.wwdx.model.WwdxThemeModel;
import com.wwdx.model.dto.BookBusinessDTO;
import com.wwdx.model.dto.ThemeBusinessDTO;
import com.wwdx.service.ProductServiceI;
import com.wwdx.utils.ConstantHolder;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 产品业务类
 * @author liguoliang
 * @date 2018/7/31 10:37
 */
@Service
public class ProductServiceImpl extends BaseCommons implements ProductServiceI {

    @Override
    public List<ThemeBusinessDTO.ThemeListDTO> themeList(List<WwdxThemeModel> themeModels, String openId,String merchantId){
        List<ThemeBusinessDTO.ThemeListDTO> list = Lists.newArrayListWithExpectedSize(themeModels.size());
        themeModels.forEach(wwdxThemeModel -> {
            ThemeBusinessDTO.ThemeListDTO dto = new ThemeBusinessDTO.ThemeListDTO();
            BeanUtils.copyProperties(wwdxThemeModel,dto);
            String[] bookList = wwdxThemeModel.getBookList().split(",");
            //书籍数量
            dto.setBookCount((long)bookList.length);
            //是否购买
            dto.setIsorder(userService.isorder(wwdxThemeModel.getThemeid(),openId,merchantId));
            //主题点击量
            Double score = zSetOperations.score(ConstantHolder.THEME_SCORE_LIST, wwdxThemeModel.getThemeid().toString());
            dto.setClickCount(score == null ? 0L : score.longValue());
            list.add(dto);
        });
        return list;
    }

    @Override
    public ThemeBusinessDTO.ThemeDetailDTO themeDetail(WwdxThemeModel model, String openId,String merchantId){
        ThemeBusinessDTO.ThemeDetailDTO dto = new ThemeBusinessDTO.ThemeDetailDTO();
        BeanUtils.copyProperties(model,dto);
        String[] bookList = model.getBookList().split(",");
        //书籍数量
        dto.setBookCount((long)bookList.length);
        Long themeid = model.getThemeid();
        //是否购买
        dto.setIsorder(userService.isorder(themeid, openId,merchantId));
        //是否为书架
        dto.setIsbookshelf(userService.isbookshelf(themeid,openId));
        //书籍列表
        WwdxBookModel wwdxBookModel = WwdxBookModel.builder().themeid(model.getThemeid()).status(0).type(0).build();
        dto.setBookList(this.bookList(wwdxBookService.selectModelList(wwdxBookModel,1,500),null,null));
        //主题点击量
        Double score = zSetOperations.score(ConstantHolder.THEME_SCORE_LIST, themeid.toString());
        dto.setClickCount(score == null ? 0L : score.longValue());
        //猜你喜欢(只有精品课才有)
        if (model.getType() == 0){
            model = WwdxThemeModel.builder().type(0).status(0).random(true).build();
            dto.setRecommendList(this.themeList(wwdxThemeService.selectModelList(model,1,2),openId,merchantId));
        }
        return dto;
    }

    @Override
    public List<BookBusinessDTO.BookListDTO> bookList(WwdxBookModel wwdxBookModel, String openId,String merchantId ,Integer page, Integer rows){
        return this.bookList(wwdxBookService.selectModelList(wwdxBookModel,page,rows),openId,merchantId);
    }

    @Override
    public List<BookBusinessDTO.BookListDTO> bookList(List<WwdxBookModel> bookModels, String openId,String merchantId){
        List<BookBusinessDTO.BookListDTO> bookListDTOS = Lists.newArrayListWithExpectedSize(bookModels.size());
        bookModels.forEach(bookModel -> {
            BookBusinessDTO.BookListDTO dto = new BookBusinessDTO.BookListDTO();
            BeanUtils.copyProperties(bookModel,dto);
            Long bookid = bookModel.getBookid();
            //是否购买
            dto.setIsorder(userService.isorder(bookid, openId,merchantId));
            bookListDTOS.add(dto);
        });
        return bookListDTOS;
    }

    @Override
    public BookBusinessDTO.BookDetailDTO bookDetail(Long bookid, String openId,String merchantId){
        WwdxBookModel wwdxBookModel = wwdxBookService.getByBookId(bookid);
        BookBusinessDTO.BookDetailDTO dto = new BookBusinessDTO.BookDetailDTO();
        BeanUtils.copyProperties(wwdxBookModel,dto);
        //书籍下才有撰稿人
        Integer type = wwdxBookModel.getType();
        if (type == 0){
            //撰稿人
            dto.setCopywriteInfo(wwdxCopywriterService.getById(wwdxBookModel.getCopywriter()));
        }else {
            //章节列表
            dto.setChapterList(wwdxBookChapterService.findBookChapterList(bookid));
        }
        //是否购买
        dto.setIsorder(userService.isorder(bookid, openId,merchantId));
        //音频转述师
        dto.setVideoauthorInfo(wwdxCopywriterService.getById(wwdxBookModel.getVideoauthorid()));
        //是否为书架
        dto.setIsbookshelf(userService.isbookshelf(bookid,openId));
        //猜你喜欢
        wwdxBookModel = WwdxBookModel.builder().status(0).type(0).random(true).build();
        dto.setRecommendList(this.bookList(wwdxBookService.selectModelList(wwdxBookModel,1,2),openId,merchantId));
        return dto;
    }
}